package com.example.final_project.Comm.Activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.ejlchina.okhttps.OkHttps;
import com.example.final_project.Comm.entry.CommContent;
import com.example.final_project.MainView;
import com.example.final_project.R;

import java.util.List;

public class ContentActivity extends AppCompatActivity {

    private ImageView back,img1,img2,img3;
    private TextView title,author;
    private Context context;
    private String simple;
    public static final int INIT=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_content);
        Intent intent = this.getIntent();
        simple = intent.getStringExtra("name");
        findById();
        getData();

        //返回点击事件
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(ContentActivity.this, MainView.class);
                intent1.setAction("backToComm");
                startActivity(intent1);
            }
        });

    }

    private Handler handler = new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case INIT:
                    List<CommContent> list = (List<CommContent>)msg.obj;
                    CommContent commContent = list.get(0);
                    title.setText(commContent.getContent());
                    author.setText(commContent.getName());
                    Glide.with(context).load("http://10.150.11.9:8086/chengcar/banners/"+commContent.getImg1()).into(img1);
                    Glide.with(context).load("http://10.150.11.9:8086/chengcar/banners/"+commContent.getImg2()).into(img2);
                    Glide.with(context).load("http://10.150.11.9:8086/chengcar/banners/"+commContent.getImg3()).into(img3);
                    break;
            }
        }
    };

    public void getData(){
        OkHttps.async("http://10.150.11.9:8086/chengcar/commcontent/")
                .bind(context)
                .addUrlPara("name",simple)
                .setOnResList(CommContent.class,ccontent ->{
                    Message message = Message.obtain();
                    message.what = INIT;
                    message.obj = ccontent;
                    handler.sendMessage(message);
                })
                .get();
    }

    public void findById(){
        back = this.findViewById(R.id.Ccomm_back);
        img1 = this.findViewById(R.id.Ccomm_img1);
        img2 = this.findViewById(R.id.Ccomm_img2);
        img3 = this.findViewById(R.id.Ccomm_img3);
        title = this.findViewById(R.id.Ccomm_title);
        author = this.findViewById(R.id.Ccomm_author);
        context = getBaseContext();
    }

}